/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-05-13
*/
package com.rzico.basics.service;

import com.rzico.base.BaseMapper;
import com.rzico.base.impl.BaseServiceImpl;
import com.rzico.basics.entity.*;
import com.rzico.basics.mapper.DistributionItemMapper;
import com.rzico.basics.mapper.ProductMapper;
import com.rzico.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rzico.basics.mapper.DistributionMapper;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <pre>
 * 分销设置业务类
 * </pre>
 *
 * @author Rzico Boot
 * @version 1.0
 */
@Service
public class DistributionService extends BaseServiceImpl<Distribution, String> {

    @Autowired
    private DistributionMapper distributionMapper;

    @Autowired
    private DistributionItemMapper distributionItemMapper;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public BaseMapper<Distribution, String> getMapper() {
        return distributionMapper;
    }

    public Distribution getDefault(Long enterpriseId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("status",0);
        params.put("type",0);
        params.put("enterpriseId",enterpriseId);
        List<Distribution> list = distributionMapper.selectList(params);
        if (list.size()>0) {
            return list.get(0);
        } else {
            Distribution distribution = new Distribution();
            distribution.setEnterpriseId(enterpriseId);
            distribution.setItemList(new ArrayList<>());
            distribution.setAgainRate(BigDecimal.ZERO);
            distribution.setIsAchieve(false);
            distribution.setAgent1(BigDecimal.ZERO);
            distribution.setPeerRate(BigDecimal.ZERO);
            distribution.setPercent1(BigDecimal.ZERO);
            distribution.setPercent2(BigDecimal.ZERO);
            distribution.setGiven(BigDecimal.ZERO);
            distribution.setBonusRate(BigDecimal.ZERO);
            distribution.setType(0);
            distribution.setName("默认规则");
            distribution.setStatus(0);
            distribution.setId(0L);
            return distribution;
        }
    };



    @Transactional(rollbackFor = Exception.class)
    public int save(Distribution distribution) {

        int affectCount = super.insertUseGeneratedKeys(distribution);

        if (distribution.getItemList()!=null) {

            for (DistributionItem item : distribution.getItemList()) {
                if (item.getWarehouseCoupon()==null) {
                    item.setWarehouseCoupon(false);
                }
                if (item.getWarehouseRate()==null) {
                    item.setWarehouseRate(BigDecimal.ZERO);
                }

                item.setDistributionId(distribution.getId());
                distributionItemMapper.insertUseGeneratedKeys(item);
            }

        }
        return affectCount;

    }

    @Transactional(rollbackFor = Exception.class)
    public int update(Distribution distribution) {

        int affectCount = super.updateByPrimaryKeySelective(distribution);

        DistributionItem delItem = new DistributionItem();

        delItem.setDistributionId(distribution.getId());
        distributionItemMapper.delete(delItem);


        if (distribution.getItemList()!=null) {

            for (DistributionItem item : distribution.getItemList()) {
                if (item.getWarehouseCoupon()==null) {
                    item.setWarehouseCoupon(false);
                }
                if (item.getWarehouseRate()==null) {
                    item.setWarehouseRate(BigDecimal.ZERO);
                }

                item.setDistributionId(distribution.getId());
                distributionItemMapper.insertUseGeneratedKeys(item);
            }

        }

        return affectCount;

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByIds(Object[] ids) {

        int rw = 0;
        for (Object id:ids) {
            Map<String,Object> params = new HashMap<>();
            params.put("distributionId",id);
            Integer w = productMapper.selectRowCount(params);
            if (w>0) {
                throw new CustomException("策略有被使用");
            }
            rw = rw + super.deleteByPrimaryKey(id);

            DistributionItem distributionItem = new DistributionItem();
            distributionItem.setDistributionId(Long.parseLong(id.toString()));
            distributionItemMapper.delete(distributionItem);
        }

        return rw;

    }


}
